package cn.gcyl.module.biz.service.subscriptioninformation;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.gcyl.module.biz.controller.admin.subscriptioninformation.vo.*;
import cn.gcyl.module.biz.dal.dataobject.subscriptioninformation.SubscriptionInformationDO;
import cn.gcyl.framework.common.pojo.PageResult;
import cn.gcyl.framework.common.pojo.PageParam;
import cn.gcyl.framework.common.util.object.BeanUtils;

import cn.gcyl.module.biz.dal.mysql.subscriptioninformation.SubscriptionInformationMapper;

import static cn.gcyl.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.gcyl.framework.common.util.collection.CollectionUtils.convertList;
import static cn.gcyl.framework.common.util.collection.CollectionUtils.diffList;
import static cn.gcyl.module.system.enums.ErrorCodeConstants.SUBSCRIPTION_INFORMATION_NOT_EXISTS;

/**
 * 订阅信息 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class SubscriptionInformationServiceImpl implements SubscriptionInformationService {

    @Resource
    private SubscriptionInformationMapper subscriptionInformationMapper;

    @Override
    public Long createSubscriptionInformation(SubscriptionInformationSaveReqVO createReqVO) {
        // 插入
        SubscriptionInformationDO subscriptionInformation = BeanUtils.toBean(createReqVO, SubscriptionInformationDO.class);
        subscriptionInformationMapper.insert(subscriptionInformation);

        // 返回
        return subscriptionInformation.getId();
    }

    @Override
    public void updateSubscriptionInformation(SubscriptionInformationSaveReqVO updateReqVO) {
        // 校验存在
        validateSubscriptionInformationExists(updateReqVO.getId());
        // 更新
        SubscriptionInformationDO updateObj = BeanUtils.toBean(updateReqVO, SubscriptionInformationDO.class);
        subscriptionInformationMapper.updateById(updateObj);
    }

    @Override
    public void deleteSubscriptionInformation(Long id) {
        // 校验存在
        validateSubscriptionInformationExists(id);
        // 删除
        subscriptionInformationMapper.deleteById(id);
    }

    @Override
        public void deleteSubscriptionInformationListByIds(List<Long> ids) {
        // 删除
        subscriptionInformationMapper.deleteByIds(ids);
        }


    private void validateSubscriptionInformationExists(Long id) {
        if (subscriptionInformationMapper.selectById(id) == null) {
            throw exception(SUBSCRIPTION_INFORMATION_NOT_EXISTS);
        }
    }

    @Override
    public SubscriptionInformationDO getSubscriptionInformation(Long id) {
        return subscriptionInformationMapper.selectById(id);
    }

    @Override
    public PageResult<SubscriptionInformationDO> getSubscriptionInformationPage(SubscriptionInformationPageReqVO pageReqVO) {
        return subscriptionInformationMapper.selectPage(pageReqVO);
    }

}